package com.hengda.museumonline.utils;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.amap.api.maps.model.Marker;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.hengda.museumonline.MyApplication;
import com.hengda.museumonline.R;
import com.hengda.museumonline.adapter.NoticeAdapter;
import com.hengda.museumonline.entity.CityChengeBean;
import com.hengda.museumonline.entity.ExhibitionBean;
import com.hengda.museumonline.http.CallServer;
import com.hengda.museumonline.ui.MainActivity;
import com.hengda.museumonline.ui.MuseumContentActivity;
import com.litesuits.common.utils.PackageUtil;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import static com.yolanda.nohttp.NoHttp.getContext;

/**
 * @author：JFWU on 2016/10/18 09:27
 * @E-mail：jeffy12138@126.com
 */
public class ExhibitionUtil {
    private PopupWindow mPop;
    private View contentView = null;
    private MainActivity mainActivity;
    private ExhibitionBean exhibitionBean;
    private View view;
    private int num = 0;
    private Marker marker;

    public ExhibitionUtil(MainActivity mainActivity, Marker marker, View view) {
        this.mainActivity = mainActivity;
        this.view = view;
        this.marker = marker;
        getExhibitionfromNet(Integer.parseInt(marker.getSnippet()));
    }

    public int getNum() {
        return num;
    }

    private void getExhibitionfromNet(int did) {
        Request request = NoHttp.createStringRequest(MyApplication.url + "showroom/museums", RequestMethod.GET);
        request.add("p", "a");
        request.add("v", PackageUtil.getAppPackageInfo(mainActivity).versionName);
        request.add("did", did);
        CallServer.getRequestInstance().add(mainActivity, 0, request, new OnResponseListener() {
            @Override
            public void onStart(int what) {
            }

            @Override
            public void onSucceed(int what, Response response) {
                Gson gson = new Gson();
                exhibitionBean = gson.fromJson(response.get().toString(), ExhibitionBean.class);
                if (exhibitionBean != null && exhibitionBean.getData() != null && exhibitionBean.getData().size() > 0) {
                    if (exhibitionBean.getData().get(0) != null &&
                            exhibitionBean.getData().get(0).getList() != null) {
                        if (exhibitionBean.getData().get(0).getList().size() == 1) {
                            num = 1;
                            getExhibition(1, exhibitionBean);
                            marker.showInfoWindow();
                        } else if (exhibitionBean.getData().get(0).getList().size() == 2) {
                            num = 2;
                            getExhibition(2, exhibitionBean);
                            marker.showInfoWindow();
                        } else if (exhibitionBean.getData().get(0).getList().size() >= 3) {
                            num = 3;
                            getExhibition(3, exhibitionBean);
                            marker.setSnippet(exhibitionBean.getData().get(0).getList().size() + "个展览");
                            marker.showInfoWindow();
                        }
                    } else {
                        marker.setSnippet("0个展览");
                        marker.showInfoWindow();
                    }
                } else {
                    marker.setSnippet("0个展览");
                    marker.showInfoWindow();
                }
            }

            @Override
            public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
                System.out.println("onFailed:" + exception);
                marker.setSnippet("0个展览");
                marker.showInfoWindow();
            }

            @Override
            public void onFinish(int what) {
            }
        });
    }

    public void dismiss() {
        if (mPop != null && mPop.isShowing()) {
            mPop.dismiss();
        }
    }

    private void getExhibition(int i, final ExhibitionBean exhibitionBean) {
        if (mPop != null && mPop.isShowing()) {
            mPop.dismiss();
        }

        if (1 == i) {
            contentView = LayoutInflater.from(mainActivity).inflate(R.layout.exhibition1_pop, null);
            TextView title = (TextView) contentView.findViewById(R.id.title);
            TextView days = (TextView) contentView.findViewById(R.id.days);
            TextView hits = (TextView) contentView.findViewById(R.id.hits);
            ImageView itemBackground = (ImageView) contentView.findViewById(R.id.item_background);
            title.setText(exhibitionBean.getData().get(0).getList().get(0).getTitle());
            days.setText(exhibitionBean.getData().get(0).getList().get(0).getDays() + "");
            hits.setText(exhibitionBean.getData().get(0).getList().get(0).getHits());
            Glide.with(getContext()).load(exhibitionBean.getData().get(0).getList().get(0).getItem_background()).into(itemBackground);
            contentView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mPop.dismiss();
                    Bundle bundle = new Bundle();
                    bundle.putString("id", exhibitionBean.getData().get(0).getList().get(0).getId());
                    Intent intent = new Intent(getContext(), MuseumContentActivity.class);
                    intent.putExtras(bundle);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    getContext().startActivity(intent);
                }
            });
        } else if (2 == i) {
            contentView = LayoutInflater.from(mainActivity).inflate(R.layout.exhibition2_pop, null);
            LinearLayout linearLayout1 = (LinearLayout) contentView.findViewById(R.id.exhibition1);
            TextView title = (TextView) linearLayout1.findViewById(R.id.title);
            TextView days = (TextView) linearLayout1.findViewById(R.id.days);
            TextView hits = (TextView) linearLayout1.findViewById(R.id.hits);
            ImageView itemBackground = (ImageView) linearLayout1.findViewById(R.id.item_background);
            title.setText(exhibitionBean.getData().get(0).getList().get(0).getTitle());
            days.setText(exhibitionBean.getData().get(0).getList().get(0).getDays() + "");
            hits.setText(exhibitionBean.getData().get(0).getList().get(0).getHits());
            Glide.with(getContext()).load(exhibitionBean.getData().get(0).getList().get(0).getItem_background()).into(itemBackground);
            linearLayout1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mPop.dismiss();
                    Bundle bundle = new Bundle();
                    bundle.putString("id", exhibitionBean.getData().get(0).getList().get(0).getId());
                    Intent intent = new Intent(getContext(), MuseumContentActivity.class);
                    intent.putExtras(bundle);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    getContext().startActivity(intent);
                }
            });
            LinearLayout linearLayout2 = (LinearLayout) contentView.findViewById(R.id.exhibition2);
            TextView title1 = (TextView) linearLayout2.findViewById(R.id.title1);
            TextView days1 = (TextView) linearLayout2.findViewById(R.id.days1);
            TextView hits1 = (TextView) linearLayout2.findViewById(R.id.hits1);
            ImageView itemBackground1 = (ImageView) linearLayout2.findViewById(R.id.item_background1);
            title1.setText(exhibitionBean.getData().get(0).getList().get(1).getTitle());
            days1.setText(exhibitionBean.getData().get(0).getList().get(1).getDays() + "");
            hits1.setText(exhibitionBean.getData().get(0).getList().get(1).getHits());
            Glide.with(getContext()).load(exhibitionBean.getData().get(0).getList().get(1).getItem_background()).into(itemBackground1);
            linearLayout2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mPop.dismiss();
                    Bundle bundle = new Bundle();
                    bundle.putString("id", exhibitionBean.getData().get(0).getList().get(1).getId());
                    Intent intent = new Intent(getContext(), MuseumContentActivity.class);
                    intent.putExtras(bundle);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    getContext().startActivity(intent);
                }
            });
        } else {
            contentView = LayoutInflater.from(mainActivity).inflate(R.layout.exhibition3_pop, null);
            RecyclerView recyclerView = (RecyclerView) contentView.findViewById(R.id.recyclerview);
            recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
            recyclerView.setAdapter(new NoticeAdapter(mainActivity, exhibitionBean));
        }
        mPop = new PopupWindow(contentView);
        mPop.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPop.setBackgroundDrawable(new BitmapDrawable());
        mPop.setOutsideTouchable(true);
        mPop.showAtLocation(view, Gravity.BOTTOM, 0, 200);
    }
}
